<?php
namespace Admin\Controller;
class MenuController extends AdminController {
    //菜单列表
    public function index(){
        $data = D('Data')->get_all_data('Menu','',10,'id desc');
        $this->assign('_page',$data['_page']);
        $this->assign('_menu',$data['data']);
        $this->display();
    }
    //菜单添加
    public function add(){
    	if(IS_POST){
    		if(!I('hide')){
    			$_POST['hide'] = 1;
    		}
    		if(D('Menu')->create()){
    			if(D('Menu')->add()){
    				$this->success('添加成功',U('index'));
    			}else{
    				$this->error('添加失败');
    			}
    		}else{
    			$this->error(D('Menu')->getError());
    		}
    	}else{
    		//获取所有顶级菜单
    		$where = [
    			'pid'	=>	0,
    		];
    		$data = D('Data')->get_all_data('Menu',$where);
    		$this->assign('data',$data['data']);
    		$this->display();
    	}
    }
    //菜单删除
    public function del(){
    	if(D('Data')->data_del('Menu')){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败,'.D('Data')->getError());
    	}
    }
    //菜单编辑
    public function edit(){
    	if(IS_POST){
    		if(D('Menu')->create()){
    			if(D('Menu')->save()){
    				$this->success('编辑成功',U('index'));
    			}else{
    				$this->error('编辑失败');
    			}
    		}else{
    			$this->error(D('Menu')->getError());
    		}
    	}else{
    		//获取之前的数据
    		$where = [
    			'id'	=>	intval(I('id')),
    		];
    		$data = D('Data')->get_row_data('Menu',$where);
    		//获取所有的顶级菜单
    		$level = D('Data')->get_all_data('Menu',array('pid'=>0));
    		$this->assign('data',$data);
    		$this->assign('level',$level['data']);
    		$this->display();
    	}
    }
}
